<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<jsp:include page="../../include/top.jsp"/>
<script type="text/javascript">
	var editorIntro;

	KindEditor.options.filterMode = false;

	KindEditor.ready(function (K) {
		editorIntro = K.create('textarea[name="content"]', {
			filterMode: true,
			htmlTags: {
				span: ['.color'],
				p: [],
				strong: [],
				a: ["href", "target"],
				//br: [],
				img: ["src", "width", "height"],
				input: ['onclick', 'click', 'type', 'value', 'url', 'data-toggle', 'href', 'id', 'name', 'alt', 'data-ke-onclick', 'href', '/'],
				embed: ['src', 'name', 'id', 'width', 'height', 'type', 'loop', 'autostart', 'quality', 'style', 'align', 'allowscriptaccess', 'flashvars', 'allowfullscreen', '/'],
				vote: ['vote', '/'],
				video: ['id', 'name', 'src', 'controls', 'width', 'height']
			},
			newlineTag: "p",
			uploadJson: '${BasePath}/base/image/upload',
			items: ['undo', 'redo', 'image', 'multiimage', 'forecolor', 'bold', 'link', 'source', 'preview', 'fullscreen'],
			afterChange: function () {
				this.sync();
			}
		});
	});
</script>
<div class="main_body">
	<div class="span9 page_message">
		<section id="contents">
			<ul class="tab-nav nav">
				<li><a href="${BasePath}/order/institution/list?${PublicParams}">机构列表<span
						class="arrow fa fa-sort-up"></span></a></li>
				<li class="current"><a href="">机构添加/修改<span class="arrow fa fa-sort-up"></span></a></li>
			</ul>
			<div class="tab-content">
				<div id="tab1" class="tab-pane in tab1">
					<!-- 表单 -->
					<form class="form-horizontal" name="myForm" id="myForm" action="${BasePath}/order/institution/save"
					      method="post">
						<input type="hidden" name="id" value="${bean.id }"/>
						<input type="hidden" name="type" value="${type }"/>
						<input type="hidden" name="itemId" value="${itemId }"/>
						<div class="form-item cf toggle-name">
							<label class="item-label">
								<span class="need_flag">*</span>
								所属机构：
								<span class="check-tips">0:默认无上级机构,不为0代表为分院</span>
							</label>
							<div class="controls">
								<input class="input-xlarge focused" id="parentId" name="parentId" type="text" value="${parentId != null?parentId:0}">
							</div>
						</div>
						<div class="form-item cf toggle-name">
							<label class="item-label">
								<span class="need_flag">*</span>
								名称：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="name" name="name"
								       type="text" value="${bean.name }">
							</div>
						</div>
						<div class="form-item cf toggle-idInstitution">
							<label class="item-label">
								<span class="need_flag">*</span>
								机构性质：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<select id="institutionType" name="institutionType">
									<option value="0" <c:if test="${base.institutionType==0}">selected</c:if>>私立
									</option>
									<option value="1" <c:if test="${base.institutionType==1}">selected</c:if>>公立
									</option>
								</select>
							</div>
						</div>
						<div class="form-item cf toggle-idInstitution">
							<label class="item-label">
								<span class="need_flag">*</span>
								省市区：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<select id="province" name="province" onchange="initCities('province','city','district')">
									<option value="-1">请选择</option>
								</select>
								<select id="city" name="city" onchange="initDistricts('province','city','district')">
									<option value="-1">请选择</option>
								</select>
								<select id="district" name="district">
									<option value="-1">请选择</option>
								</select>
							</div>
						</div>
						<div class="form-item cf toggle-address">
							<label class="item-label">
								<span class="need_flag">*</span>
								地址：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="address" name="address"
								       type="text" value="${bean.address }">
							</div>
						</div>
						<div class="form-item cf toggle-contact">
							<label class="item-label">
								<span class="need_flag">*</span>
								联系人：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="contact" name="contact"
								       type="text" value="${bean.contact }">
							</div>
						</div>
						<div class="form-item cf toggle-phone">
							<label class="item-label">
								<span class="need_flag">*</span>
								联系电话：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="phone" name="phone"
								       type="text" value="${bean.phone }">
							</div>
						</div>
						<div class="form-item cf toggle-mobile">
							<label class="item-label">
								<span class="need_flag">*</span>
								手机：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="mobile" name="mobile"
								       type="text" value="${bean.mobile}">
							</div>
						</div>
						<div class="form-item cf toggle-isHot">
							<label class="item-label">
								<span class="need_flag">*</span>
								推荐：用在网站上
								<span class="check-tips"></span>
							</label>
							<div class="controls">
								<select id="isHot" name="isHot">
									<option value="0"  <c:if test="${bean.isHot=='0'}">selected</c:if>>不推荐</option>
									<option value="1" <c:if test="${bean.isHot=='1'}">selected</c:if>>推荐</option>
								</select>
							</div>
						</div>
						<div class="form-item cf toggle-toWebsite">
							<label class="item-label">
								<span class="need_flag">*</span>
								网站显示：是否用在网站上
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<select id="toWebsite" name="toWebsite">
									<option value="0" <c:if test="${bean.toWebsite eq '0'}">selected</c:if>>不</option>
									<option value="1" <c:if test="${bean.toWebsite eq '1'}">selected</c:if>>是</option>
								</select>
							</div>
						</div>
						<div class="form-item cf toggle-workTime">
							<label class="item-label">
								<span class="need_flag">*</span>
								营业时间：
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input class="input-xlarge focused" id="workTime" name="workTime" type="text" value="${bean.workTime}">
							</div>
						</div>
                        <div class="form-item cf toggle-workTime">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                描述信息：
                                <span class="check-tips">必须在40与50字之间！</span>
                            </label>

                            <div class="controls">
                                <textarea name="desc" class="input-xlarge focused" id="desc">${bean.desc}</textarea>
                            </div>
                        </div>
						<div class="form-item cf toggle-cover">
							<label class="item-label">
								封面图片
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<input type="hidden" name="images" value="${bean.images}" id="images"/>
								<div class="upload-img-box">
                                    <c:forEach items="${bean.images.split(\";\")}" var="v">
                                        <c:if test="${v != \"\"}">
                                        <div class="upload-pre-item2"><img width="180" height="100" src="${v}"/><input type="hidden" name="hideImages" value="${v}"/><input type="button" class="imageDelete btn btn-small" onclick="deleteImage(this)" value="删除"/></div>
                                        </c:if>
                                    </c:forEach>
								</div>
								<div class="uploadrow2" title="点击修改图片">
									<input type="file" name="file" id="upload_picture_cover"/>
									<input type="hidden" name="cover" id="cover_id_cover"/>
								</div>
								<script type="text/javascript">
									$(function () {
										/* 初始化上传插件 */
										$("#upload_picture_cover").uploadify({
											"height": 100,
											"swf": "${BasePath}/Public/static/uploadify/uploadify.swf",
											"fileObjName": "file",
											"buttonText": "上传图片",
											"uploader": "${BasePath}/base/image/upload;jsessionid=<%=session.getId()%>",
											"width": 180,
											'removeTimeout': 1,
											'fileTypeExts': '*.jpg; *.png; *.gif;',
											'onUploadSuccess': uploadPicturecover
										});
										function uploadPicturecover(file, data) {
											var data = $.parseJSON(data);
											var src = '';
											if (data.errno) {
												src = data.url || '' + data.path;
												$("#cover_id_cover").parent().parent().find('.upload-img-box').append(
														'<div class="upload-pre-item2"><img width="180" height="100" src="' + src + '"/><input type="hidden" name="hideImages" value="' + data.uri +'"/><input type="button" class="imageDelete btn btn-small" onclick="deleteImage(this)" value="删除"/></div>'
												);
												<%--$('.weixin-cover-pic').attr('src', "${BasePath}" + src);--%>
												$("#images").val($("#images").val() + ";" + data.uri);
											} else {
												updateAlert(data.errdesc);
												setTimeout(function () {
													$('#top-alert').find('button').click();
													$(that).removeClass('disabled').prop('disabled', false);
												}, 1500);
											}
										}
									});

                                    function deleteImage(obj){
                                        $(obj).parent().remove();
                                        var objs = $("input[name=hideImages]");
                                        var arr = [];
                                        for(i=0;i<objs.length;i++){
                                            arr.push($(objs[i]).val());
                                        }
                                        $("#images").val(arr.join(";"));
                                    }
								</script>
							</div>
						</div>
						<div class="form-item cf toggle-content">
							<label class="item-label">
								内容
								<span class="check-tips"></span>
							</label>

							<div class="controls">
								<label class="textarea">
									<textarea name="content">${bean.content}</textarea>
								</label>
							</div>
						</div>
						<button class="btn submit-btn" id="submit" type="button" onclick="submitForm()" >确 定</button>
					</form>
				</div>
			</div>
		</section>
	</div>
</div>
<script type="text/javascript">
	initProvince("province","city","district", '${bean.province}');
	initCities("province","city","district", '${bean.city}');
	initDistricts("province","city","district", '${bean.district}');

	function submitForm() {
        var parentId = $("#parentId").val();
        if (parentId.trim() == ""  ) {
            $("#parentId").val(0);
        }else if(isNaN(parentId)){
            layer.msg('所属机构必须未数字!', {icon: 5});
            return false;
        }
        var name = $("#name").val();
        if (name.trim() == "") {
            layer.msg('用户名不能为空!', {icon: 5});
            return false;
        }

        var province = $("#province").val();
        if (province.trim()=='-1') {
            layer.msg('请选择省市!', {icon: 5});
            return false;
        }
        var city = $("#city").val();
        if (city.trim() == "") {
            layer.msg('请选择省市!', {icon: 5});
            return false;
        }
        var district = $("#district").val();
        if (district.trim() == "") {
            layer.msg('请选择省市!', {icon: 5});
            return false;
        }
        var address = $("#address").val();
        if (address.trim() == "") {
            layer.msg('地址不能为空!', {icon: 5});
            return false;
        }
        var contact = $("#contact").val();
        if (contact.trim() == "") {
            layer.msg('联系人不能为空!', {icon: 5});
            return false;
        }
        var phone = $("#phone").val();
        var mobile = $("#mobile").val();
        if (phone.trim() == "" && mobile.trim() == "") {
            layer.msg('联系电话不能为空!', {icon: 5});
            return false;
        }
        var workTime = $("#workTime").val();
        if (workTime.trim() == "") {
            layer.msg('营业时间不能为空!', {icon: 5});
            return false;
        }
        var toWebsite=$('#toWebsite option:selected') .val()
        var desc = $("#desc").val();
        if (parentId==0 && toWebsite==1 && desc.trim() == "") {
            layer.msg('描述信息不能为空!', {icon: 5});
            return false;
        }else if(parentId==0 && toWebsite==1 && desc.length<40){
            layer.msg('描述信息至少40字符!', {icon: 5});
            return false;
        }
        var images= $("#images").val();
        if (parentId==0 && toWebsite==1 && images.trim() == "") {
            layer.msg('请上传封面图!', {icon: 5});
            return false;
        }

		$('#myForm').ajaxSubmit(function (data) {
            if (data.errno == 1) {
                layer.msg(data.errdesc,{icon: 5});
            } else {
                layer.msg('操作成功！');
                window.location.href = "${BasePath}/order/institution/list?${PublicParams}";
            }
		});
	}
</script>
<jsp:include page="../../include/bottom.jsp"/>
